#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT

#include <Blinker.h>
#include <SoftwareSerial.h>

SoftwareSerial SoftSerial(4, 0); 

char auth[] = "0c2422c2db8e";
char ssid[] = "204_IOT";
char pswd[] = "XUTIANZE19990919";

// 新建组件对象
BlinkerButton Button_a_on("a_on");
BlinkerButton Button_a_off("a_off");
BlinkerButton Button_l_on("l_on");
BlinkerButton Button_l_off("l_off");
BlinkerButton Button_r_on("r_on");
BlinkerButton Button_r_off("r_off");

//小爱同学支持
void miotPowerState(const String & state)
{
  BLINKER_LOG("need set power state: ", state);

  if (state == BLINKER_CMD_ON) {
  digitalWrite(LED_BUILTIN, LOW);
    SoftSerial.print("AXE");
  digitalWrite(LED_BUILTIN, HIGH);

    BlinkerMIOT.powerState("on");
    BlinkerMIOT.print();
  }
  else if (state == BLINKER_CMD_OFF) {
  digitalWrite(LED_BUILTIN, LOW);
    SoftSerial.print("AOE");
  digitalWrite(LED_BUILTIN, HIGH);

    BlinkerMIOT.powerState("off");
    BlinkerMIOT.print();
  }
}

// 按下按键即会执行该函数
void Button_a_on_callback(const String & state)
{
  digitalWrite(LED_BUILTIN, LOW);
    SoftSerial.print("AXE");
  digitalWrite(LED_BUILTIN, HIGH);
}
void Button_a_off_callback(const String & state)
{
  digitalWrite(LED_BUILTIN, LOW);
    SoftSerial.print("AOE");
  digitalWrite(LED_BUILTIN, HIGH);
}
void Button_l_on_callback(const String & state)
{
  digitalWrite(LED_BUILTIN, LOW);
  SoftSerial.print("LXE");
  digitalWrite(LED_BUILTIN, HIGH);
}
void Button_l_off_callback(const String & state)
{
  digitalWrite(LED_BUILTIN, LOW);
  SoftSerial.print("LOE");
  digitalWrite(LED_BUILTIN, HIGH);
}
void Button_r_on_callback(const String & state)
{
  digitalWrite(LED_BUILTIN, LOW);
  SoftSerial.print("RXE");
  digitalWrite(LED_BUILTIN, HIGH);
}
void Button_r_off_callback(const String & state)
{
  digitalWrite(LED_BUILTIN, LOW);
  SoftSerial.print("ROE");
  digitalWrite(LED_BUILTIN, HIGH);
}

void setup()
{
  // 初始化串口
  SoftSerial.begin(115200);

  // 初始化有LED的IO
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  // 初始化blinker
  Blinker.begin(auth, ssid, pswd);
  BlinkerMIOT.attachPowerState(miotPowerState);

  Button_a_on.attach(Button_a_on_callback);
  Button_a_off.attach(Button_a_off_callback);
  Button_l_on.attach(Button_l_on_callback);
  Button_l_off.attach(Button_l_off_callback);
  Button_r_on.attach(Button_r_on_callback);
  Button_r_off.attach(Button_r_off_callback);
}

void loop()
{
  Blinker.run();

  if ( WiFi.status() == WL_CONNECTED )
    digitalWrite(LED_BUILTIN, HIGH);
  else
    digitalWrite(LED_BUILTIN, LOW);
}
